<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/19
 * Time: 15:19
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class MaterialSpecModel
{
    const TABLE = 'material_spec';

    public function lists(array $data){
        return DB::table(self::TABLE)
            ->leftJoin('unit as u', 'u.id', '=', self::TABLE . '.unitId')
            ->where(function ($query) use ($data) {
                $query->where('materialId', $data['materialId']);
            })
            ->select(self::TABLE.'.*','u.name as unitName')
            ->get()->toArray();
    }
    ///////////////////////////////////////////////
    /**
     * @param array $data
     * @return mixed
     */
//    public function lists(array $data)
//    {
//        $result = DB::table(self::TABLE)
//            ->where(function ($query) use ($data) {
//                $query->where('materialId', $data['materialId'])->where('isDel', 0);
//            });
//        if (!empty($input['length']) && !empty($input['page'])) {
//            $limit = config('yucheng.limit');
//            $start = 0;
//            if (!empty($param['length'])) {
//                $limit = $param['length'];
//            }
//            if (!empty($param['page'])) {
//                $start = ($param['page'] - 1) * $limit;
//            }
//            $result = $result->offset($start)->limit($limit);
//        }
//        return $result->get()->toArray();
//    }

    /**
     * @param array $data
     * @return mixed
     */
    public function countLists(array $data)
    {
        return DB::table(self::TABLE)
            ->where(function ($query) use ($data) {
                $query->where('materialId', $data['materialId']);
            })
            ->count();
    }

    /**
     * @param array $check
     * @param $id
     * @return array
     */
    public function checkRepeat(array $check, $id = 0)
    {
        $result = DB::table(self::TABLE)->where($check)
            ->where(function ($query) use ($id) {
                if ($id != 0) {
                    $query->where('id', '!=', $id);
                }
            })
            ->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        $data['createTime'] = date('Y-m-d H:i:s');
        return DB::table(self::TABLE)->insert($data);
    }

    /**
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where, array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    /**
     * @param array $data
     * @return array
     */
    public function info(array $data)
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function selectLists(array $data)
    {
        return DB::table(self::TABLE)
            ->where(function ($query) use ($data) {
                $query->where('materialId', $data['materialId'])->where('isDel', 0);
            })
            ->get()->toArray();
    }

    public function addSpec(array $param)
    {
        DB::beginTransaction();
        try{
            $material = (new MaterialModel())->info(['id'=>$param['materialId']]);
            $param['categoryId'] = $material['categoryId'];
            $param['createTime'] = date('Y-m-d H:i:s');
            $this->insert($param);
            DB::commit();
        }catch(\Exception $e){
            DB::rollBack();
        }
    }

    public function delete(array $data)
    {
        return DB::table(self::TABLE)->where($data)->delete();
    }
}